﻿using Hotel.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hotel.DAL
{
    public class ShopDAL
    {
        public ShopDAL(HotelContext hotelContext)
        {
            HotelContext = hotelContext;
        }

        public HotelContext HotelContext { get; }

        /// <summary>
        /// 增加门店
        /// </summary>
        /// <param name="shop"></param>
        /// <returns></returns>
        public int ShopAdd(Shop shop)
        {
            try
            {
                HotelContext.shops.Add(shop);
                return HotelContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 门店名称不能重复
        /// </summary>
        /// <param name="shopName"></param>
        /// <returns></returns>
        public int SearchShopName(string shopName)
        {
            try
            {
                int res = HotelContext.shops.Where(x => x.Name == shopName).Count();
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 分页显示
        /// </summary>
        /// <param name="totalCount"></param>
        /// <param name="totalPage"></param>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<Shop> GetShops(out int totalCount,out int totalPage,string? name,int pageIndex,int pageSize)
        {
            try
            {
                List<Shop> list = HotelContext.shops.Where(x => x.IsDeleted == false).ToList();

                if (!string.IsNullOrEmpty(name))
                {
                    list = list.Where(x => x.Name.Contains(name)).ToList();
                }

                totalCount = list.Count;

                totalPage = (int)Math.Ceiling(totalCount * 1.0 / pageSize);

                list = list.OrderByDescending(x => x.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DelShop(int id)
        {
            try
            {
                var res = HotelContext.shops.Find(id);
                res.IsDeleted = true;
                return HotelContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 门店信息反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Shop FanShop(int id)
        {
            try
            {
                return HotelContext.shops.Find(id);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 门店修改
        /// </summary>
        /// <param name="shop"></param>
        /// <returns></returns>
        public int UpdShop(Shop shop)
        {
            try
            {
                var res = HotelContext.shops.Find(shop.Id);
                res.Name = shop.Name;
                res.State = shop.State;
                return HotelContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
